package vn.lumi.smarthome.network;

import java.util.Arrays;

import vn.lumi.smarthome.LumiHome;
import vn.lumi.smarthome.R;
import vn.lumi.smarthome.io.database.TBL_DeviceState;
import vn.lumi.smarthome.io.database.TBL_Schedule;
import vn.lumi.smarthome.ulti.Preference;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Parcel;
import android.util.Log;

public class DelSchedule extends AppMsg{

	//constants
	private static final int LENGTH = 6;
	
	//attributes
	private int Schedule_id = 0;
	private int Device_id = 0;

	private int newConfigVersion;
	

	public DelSchedule(int Msg_id, int uid, int sid, int schedule_id, int device_id) {
		super(NetMessage.CMD_APP_DEL_SCHEDULE, Msg_id, LENGTH,  uid, sid);
		// TODO Auto-generated constructor stub
		Schedule_id = schedule_id;
		Device_id = device_id;
	}
	
	@Override
	public byte[] generateByteArray() {
		// TODO Auto-generated method stub
		byte[] result = new byte[LENGTH];
		result[0] = (byte) this.Cmd_id;
		result[1] = (byte) this.Msg_id;
		result[2] = (byte) this.user_id;
		result[3] = (byte) this.session_id;
		result[4] = (byte) this.Schedule_id;
		result[5] = (byte) this.Device_id; 
		Log.e("ban tin","DelSchedule:" + Arrays.toString(result));
		return result;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		// TODO Auto-generated method stub
		dest.writeInt(this.Cmd_id);
		dest.writeInt(this.length);
		dest.writeInt(this.Msg_id);
		dest.writeInt(this.session_id);
		dest.writeInt(this.user_id);
		dest.writeInt(this.Schedule_id);
		dest.writeInt(this.Device_id);
	}

	@Override
	public void doIfSendSuccessful(Context context) {
		// TODO Auto-generated method stub
		//del schedule in db
		String selection = new String(TBL_Schedule._ID + "=?");
		String[] selectionArgs = new String[]{this.Schedule_id+""};
		
		// Xoa bang TBL_Device_State
		String [] projection = new String[]{TBL_Schedule.STATE_ID};
		Cursor cursor =context.getContentResolver().query(TBL_Schedule.CONTENT_URI_SCHEDULE, projection, selection, selectionArgs, null);
		String selection_state = new String(TBL_Schedule._ID + "=?");
		cursor.moveToFirst();
		String[] selectionArgs_state = new String[]{cursor.getInt(0)+""};
		context.getContentResolver().delete(TBL_DeviceState.CONTENT_URI_DEVICE_STATE, selection_state, selectionArgs_state);
		// Xoa bang TBL_Schedule
		context.getContentResolver().delete(TBL_Schedule.CONTENT_URI_SCHEDULE, selection, selectionArgs);
		
		//save config in preferences
		LumiHome.HC_Config_ver = this.newConfigVersion;
		Preference.saveConfigVersion(context, this.newConfigVersion);
		
		//inform UI
	    Intent intent = new Intent();
	    intent.setAction(context.getString(R.string.txt_broadcast_del_schedule_sucessful));
	    context.sendBroadcast(intent);
	}

	@Override
	public void doIfSendFail(Context context) {
		// TODO Auto-generated method stub
		//inform UI
	    Intent intent = new Intent();
	    intent.setAction(context.getString(R.string.txt_broadcast_del_schedule_failed));
	    context.sendBroadcast(intent);
	}

	@Override
	public void readFromParcel(Parcel in) {
		// TODO Auto-generated method stub
		Cmd_id = in.readInt();
		length = in.readInt();
		Msg_id = in.readInt();
		session_id = in.readInt();
		user_id = in.readInt();
		this.Schedule_id = in.readInt();
		this.Device_id = in.readInt();
	}

	@Override
	public boolean isCorrectReply(byte[] input, int length) {
		// TODO Auto-generated method stub
		boolean result = false;
		AckSchedule ack = new AckSchedule();
//		Log.e("ban tin","DelSchedule:" + "before");
		Log.e("ban tin","DelSchedule BeforeisCorrect:" + Arrays.toString(input));
		if(ack.isCorrectFormat(input, length)){
			ack.parseMessage(input, length);
			Log.e("ban tin","DelSchedule isCorrect:" + Arrays.toString(input));
			if(ack.getMsg_id() == this.Msg_id
					&&ack.getUserId() == this.getUid()) {
				this.Schedule_id = ack.getScheduleId();
				this.newConfigVersion = ack.getConfigVersion();
				result = true;
			}
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#isCorrectBusy(byte[], int)
	 */
	@Override
	public boolean isCorrectBusy(byte[] input, int length) {
		// TODO Auto-generated method stub
		boolean result =false;
		HC_ACK_BUSY ack_busy = new HC_ACK_BUSY();
		if(ack_busy.isCorrectFormatBusy(input, length)){
			ack_busy.parseMessage(input, length);
			if(ack_busy.getMsg_id() == this.Msg_id) result = true;
		}
		return result; 
	}

	/* (non-Javadoc)
	 * @see vn.lumi.smarthome.network.AppMsg#doIfHcBusying()
	 */
	@Override
	public void doIfHcBusying() {
		// TODO Auto-generated method stub
		
	}


}
